package com.atguigu.repservice.service.impl;

import com.atguigu.commonutils.ordervo.UserOrderVo;
import com.atguigu.repservice.entity.RepOrder;
import com.atguigu.repservice.entity.vo.OrderRequestVo;
import com.atguigu.repservice.entity.vo.RepOrderRequestVo;
import com.atguigu.repservice.mapper.RepOrderMapper;
import com.atguigu.repservice.service.RepOrderService;
import com.atguigu.repservice.service.client.RepUserClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZhuangLiJiao
 * @since 2021-10-23
 */
@Service
public class RepOrderServiceImpl extends ServiceImpl<RepOrderMapper, RepOrder> implements RepOrderService {

    @Autowired
    private RepOrderMapper orderMapper;

    @Autowired
    private RepUserClient userClient;


    //1.创建订单：用户发起需求
    @Override
    public String userPublishOrder(RepOrderRequestVo.UserPublishOrder userPublishOrder, String memberId) {
        RepOrder order = new RepOrder();
        String subjectId = userPublishOrder.getSubjectId();
        String subjectParentId = userPublishOrder.getSubjectParentId();
        String questionDescription = userPublishOrder.getQuestionDescription();
        System.out.println("图片：" + userPublishOrder.getImageUrl());
        if (!"".equals(userPublishOrder.getImageUrl())) {
            order.setPicture1(userPublishOrder.getImageUrl());
        }
        order.setSubjectId(subjectId);
        order.setSubjectParentId(subjectParentId);
        order.setDescription(questionDescription);

        //设置订单状态
        order.setStatus("1");

        //获取用户信息
        UserOrderVo userInfoOrder = userClient.getUserInfoOrder(memberId);
        //用户id
        order.setUserId(userInfoOrder.getId());
        //用户账号
        order.setUserMobile(userInfoOrder.getMobile());
        //用户姓名
        order.setUserName(userInfoOrder.getNickname());
        //设置地址
        order.setAddress(userPublishOrder.getAddress());
        //备注
        order.setRemark(userPublishOrder.getRemark());

        order.setCommunity(userInfoOrder.getCommunity());

        System.out.println("=================================================================");
        System.out.println(userInfoOrder);
        System.out.println("=================================================================");


        orderMapper.insert(order);
        return subjectId;
    }

    //2.条件查询带分页
    @Override
    public Page<RepOrder> pageOrderCondition(long current, long limit, OrderRequestVo.OrderQueryVo orderRequestVo) {

        Page<RepOrder> orderPage = new Page<>(current, limit);
        //构建条件
        LambdaQueryWrapper<RepOrder> wrapper = new LambdaQueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql

        String type = orderRequestVo.getType();
        String status = orderRequestVo.getStatus();
        String shopName = orderRequestVo.getShopName();
        String merchantMobile = orderRequestVo.getMerchantMobile();
        String subjectId = orderRequestVo.getSubjectId();
        String subjectParentId = orderRequestVo.getSubjectParentId();
        String communities = orderRequestVo.getCommunities();//社区
        String userName = orderRequestVo.getUserName();
        String userMobile = orderRequestVo.getUserMobile();

        String beginTime = orderRequestVo.getBeginTime();
        String endTime = orderRequestVo.getEndTime();

        //商家用户：不选状态直接查询，按照行业来查
        if (!StringUtils.isEmpty(type) && type.equals("2")) //商家用户
        {
//            wrapper.eq(RepOrder::getSubjectId,subjectId);
            if (!StringUtils.isEmpty(subjectParentId)) {
                wrapper.eq(RepOrder::getSubjectParentId, subjectParentId);
            }
        }

        //当状态为 商家用户 已经接单后


        //
//        if (!StringUtils.isEmpty(type) && type.equals("1")) //普通用户
//        {
////            wrapper.eq(RepOrder::getSubjectId,subjectId);
//            if (!StringUtils.isEmpty(subjectParentId)) {
//                wrapper.eq(RepOrder::getSubjectParentId, subjectParentId);
//            }
//        }

        //按照社区来查询
        if (!StringUtils.isEmpty(communities) && !"全部".equals(communities)) {
            wrapper.eq(RepOrder::getCommunity, communities);
        }
        //判断条件值是否为空，如果不为空拼接条件
        //订单状态
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq(RepOrder::getStatus, status);
            if (status.equals("3") || status.equals("4") || status.equals("5")) {
                //商家已经接单、服务、完成等等,使用商家的手机号进行查询
                if (!StringUtils.isEmpty(merchantMobile)) {
                    wrapper.like(RepOrder::getMerchantMobile, merchantMobile);
                }
            }
        }

        if (!StringUtils.isEmpty(shopName)) {
            wrapper.like(RepOrder::getMerchantShopName, shopName);
        }

        if (!StringUtils.isEmpty(userName)) {
            wrapper.like(RepOrder::getUserName, userName);
        }
        if (!StringUtils.isEmpty(userMobile)) {
            wrapper.like(RepOrder::getUserMobile, userMobile);
        }

        if (!StringUtils.isEmpty(beginTime)) {
            wrapper.ge(RepOrder::getCreateTime, beginTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            wrapper.le(RepOrder::getModifiedTime, endTime);
        }
        //排序
        wrapper.orderByDesc(RepOrder::getModifiedTime);

        //调用方法实现条件查询分
        orderMapper.selectPage(orderPage, wrapper);

        return orderPage;
    }
}
